package com.project.servlet;

import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class TestServlet extends HttpServlet {

    @Autowired
    private IUserService service;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //必须删掉super
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求信息的编码集，只对post对象
        req.setCharacterEncoding("utf-8");
        //设置响应信息的MIME类型和编码集
        resp.setContentType("text/html;charset=utf-8");
        //根据表单名得到表单值
        String userName = req.getParameter("userName");
        String pwd = req.getParameter("pwd");
        UserBean user = service.findByName(userName,pwd);
        if (userName.equals(user.getName())&pwd.equals(user.getPwd())){
            resp.getWriter().print("登录成功:" + userName);
        }else {
            resp.getWriter().print("登陆失败");
        }

//        //向客户端输出文本信息
//        resp.getWriter().println("hello，"+userName);
    }
}
